package com.syj.jsxjj.ui.lend.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseFragment;
import com.syj.jsxjj.http.HttpManager;
import com.syj.jsxjj.http.HttpSubscriber;
import com.syj.jsxjj.ui.lend.bean.HomeBean;
import com.syj.jsxjj.ui.lend.bean.ProductDetailBean;
import com.syj.jsxjj.ui.main.WebViewActivity;
import com.syj.jsxjj.util.ToastUtil;
import com.syj.jsxjj.widget.recycler.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 产品列表
 */
public class ProductListFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView mList;

    private MyProductListRecyclerViewAdapter adapter;

    private List<HomeBean.PlateList2Bean> list;

    public ProductListFragment() {
    }


    public static ProductListFragment newInstance(ArrayList<HomeBean.PlateList2Bean> list) {
        ProductListFragment fragment = new ProductListFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList("list", list);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = getArguments().getParcelableArrayList("list");
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_productlist_list;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void loadData() {
        mList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MyProductListRecyclerViewAdapter();
        mList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                toSubscribe(HttpManager.getApi().productInfoDetails(adapter.getData().get(position).getEid()), new HttpSubscriber<ProductDetailBean>() {
                    @Override
                    protected void _onStart() {
                        App.loadingDefault(mActivity);
                    }

                    @Override
                    protected void _onNext(ProductDetailBean productDetailBean) {
                        Intent intent = new Intent(mActivity, WebViewActivity.class);
                        intent.putExtra("url",productDetailBean.getProductInfo().getJunkLink());
                        startActivity(intent);
                    }


                    @Override
                    protected void _onError(String message) {
                        ToastUtil.showToast(message);
                    }

                    @Override
                    protected void _onCompleted() {
                        App.hideLoading();
                    }
                });
            }
        });
        adapter.addData(list);
    }

}
